package com.zxjbyte.yiyi.framework.common.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.zxjbyte.yiyi.framework.common.core.okhttp.*;
import com.zxjbyte.yiyi.framework.common.core.okhttp.callback.AbstractAsyncCallback;
import com.zxjbyte.yiyi.framework.common.domain.Pair;
import lombok.experimental.UtilityClass;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * okHttp工具
 * @Author zhangxingjia
 * @Date 2023/4/19 9:47
 * @Version: 1.0
 */
@UtilityClass
public class OkHttpUtil {

    /**
     * 发送Post请求Json数据 附加请求头
     * @param path
     * @param body
     * @param headerParams
     * @param returnClazz
     * @return
     */
    public <T> T postJson(String path, Object body, Map<String, String> headerParams, Class<T> returnClazz){
        return OkRequest.createPostJson(path, body, headerParams)
                .execute()
                .deserialize(returnClazz);
    }

    /**
     * 发送Post请求Json数据 附加请求头
     * @param path
     * @param body
     * @return
     */
    public String postJson(String path, Object body, Map<String, String> headerParams){
        return OkHttpUtil.postJson(path, body, headerParams, String.class);
    }

    /**
     * 发送Post请求Json数据
     * @param path
     * @param body
     * @return
     */
    public String postJson(String path, Object body){
        return OkHttpUtil.postJson(path, body, null);
    }

    /**
     * 发送Post请求Form数据 附加请求头
     * @param path
     * @param formParams
     * @param headerParams
     * @return
     */
    public <T> T postForm(String path, Map<String, Object> formParams, Map<String, String> headerParams, Class<T> returnClazz){
        return OkRequest.createPostForm(path, formParams, headerParams)
                .execute()
                .deserialize(returnClazz);
    }


    /**
     * 发送Post请求Form数据 附加请求头
     * @param path
     * @param formParams
     * @param headerParams
     * @return
     */
    public String postForm(String path, Map<String, Object> formParams, Map<String, String> headerParams){
        return OkHttpUtil.postForm(path, formParams, headerParams, String.class);
    }

    /**
     * 发送Post请求Form数据
     * @param path
     * @param formParams
     * @return
     */
    public String postForm(String path, Map<String, Object> formParams){
        return OkHttpUtil.postForm(path, formParams, null);
    }

    /**
     * 发送Get请求 附加请求头
     * @param path
     * @param queryParams
     * @param headerParams
     * @return
     */
    public <T> T get(String path, List<Pair> queryParams, Map<String, String> headerParams, Class<T> returnClazz){
        return OkRequest.createGet(path, queryParams, headerParams)
                .execute()
                .deserialize(returnClazz);
    }

    /**
     * 发送Get请求 附加请求头
     * @param path
     * @param queryParams
     * @param headerParams
     * @return
     */
    public String get(String path, List<Pair> queryParams, Map<String, String> headerParams){
        return OkHttpUtil.get(path, queryParams, headerParams, String.class);
    }

    /**
     * 发送Get请求
     * @param path
     * @param queryParams
     * @return
     */
    public String get(String path, List<Pair> queryParams){
        return OkHttpUtil.get(path, queryParams, null, String.class);
    }

    /**
     * 上传文件
     * @param path
     * @return
     */
    public String upload(String path, Map<String, Object> formParams){
        return OkRequest.createUpload(path, formParams).client(OkHttpCliHolder.defaultCreate().log(LoggerLevel.BASIC).build())
                .execute()
                .deseStr();
    }

    /**
     * 单上传文件
     * @param path
     * @return
     */
    public String upload(String path, File file){
        Map<String, Object> formParams = Maps.newHashMapWithExpectedSize(1);
        formParams.put("file", file);
        return OkHttpUtil.upload(path, formParams);
    }

    /**
     * 上传下载 附加请求参数，请求头
     * @param path
     * @return
     */
    public File download(String path, List<Pair> queryParams, Map<String, String> headerParams){
        return OkHttpUtil.get(path, queryParams, headerParams, File.class);
    }

    /**
     * 上传下载
     * @param path
     * @return
     */
    public File download(String path){
        return OkHttpUtil.download(path, null, null);
    }

    /**
     * 上传下载
     * @param path
     * @return
     */
    public InputStream downloadAsStream(String path, List<Pair> queryParams, Map<String, String> headerParams){
        return OkHttpUtil.get(path, queryParams, headerParams, InputStream.class);
    }

    /**
     * 上传下载
     * @param path
     * @return
     */
    public InputStream downloadAsStream(String path){
        return OkHttpUtil.downloadAsStream(path,null, null);
    }

    /**
     * 异步上传文件
     * @param path
     * @param file
     * @param callback 文件上传进度
     */
    public void uploadAsync(String path, File file, AbstractAsyncCallback callback){
        Map<String, Object> formParams = Maps.newHashMapWithExpectedSize(1);
        formParams.put("file", file);
        Map<String, String> headerParams = Maps.newHashMap();
        headerParams.put("Content-Length", StrUtil.toString(FileUtil.size(file)));
        OkRequest.createUpload(path, formParams).headers(headerParams).executeAsync(callback);
    }

    /**
     * 异步文件下载 附加请求参数，请求头
     * @param path
     * @return
     */
    public void downloadAsync(String path, List<Pair> queryParams, Map<String, String> headerParams, AbstractAsyncCallback callback){
        OkRequest.createDownload(path, queryParams, headerParams).executeAsync(callback);
    }

    /**
     * 异步文件下载
     * @param path
     * @return
     */
    public void downloadAsync(String path, AbstractAsyncCallback callback){
        OkHttpUtil.downloadAsync(path, null, null, callback);
    }

    /**
     * 异步发送Post请求Json数据 附加请求头
     * @param path
     * @param body
     * @param headerParams
     * @return
     */
    public <T> void postJsonAsync(String path, Object body, Map<String, String> headerParams, AbstractAsyncCallback callback){
        OkRequest.createPostJson(path, body, headerParams).executeAsync(callback);
    }

    /**
     * 异步发送Post请求Json数据
     * @param path
     * @param body
     * @return
     */
    public <T> void postJsonAsync(String path, Object body, AbstractAsyncCallback callback){
        OkHttpUtil.postJsonAsync(path, body, null, callback);
    }

}
